package com.cheer.ba.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.model.OilSendModel;
import com.cheer.ba.presenter.impl.CardGroupPresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.activity.CommentListAty;
import com.cheer.ba.view.activity.OilDetailAty;
import com.cheer.ba.view.activity.OilOrderDetailAty;
import com.cheer.ba.view.activity.OilSetMealAty;
import com.cheer.ba.view.activity.SearchOilAty;
import com.cheer.ba.view.activity.TransitAty;
import com.cheer.ba.view.adapter.MyOrderAdp;
import com.cheer.ba.view.animation.CustomAnimation;
import com.cheer.ba.view.base.BaseCommFrg;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的订单
 * Created by Hq on 2017/7/3.
 */
public class MyOrderFrg extends BaseCommFrg {

    @BindView(R.id.id_oil_order_xrefreshv)
    XRefreshView mXRefreshView;
    //数据加载错误
    @BindView(R.id.id_my_order_error_data)
    LinearLayout mLinearError;

    @BindView(R.id.id_oil_order_recyclev)
    RecyclerView mRecycleView;
    private MyOrderAdp mOilOrderAdp;
    //存储柴油信息
    private List<MyOrderModel> mSaveMyOrders = new ArrayList<MyOrderModel>();

    //类型
    private int categroy;

    /**
     * @param type -1 全部 1待支付 2已支付 3 完成
     * @return
     */
    public static MyOrderFrg newInstace(int type) {
        MyOrderFrg frg = new MyOrderFrg();
        Bundle bundle = new Bundle();
        bundle.putInt(IntentKey.General.KEY_TYPE, type);
        frg.setArguments(bundle);
        return frg;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categroy = getArguments().getInt(IntentKey.General.KEY_TYPE, Constants.DEFAULTVALUE);
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_my_order_list, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return !mSaveMyOrders.isEmpty();
    }

    @Override
    public void initPresenter() {
        presenter = new CardGroupPresenter();
        ((CardGroupPresenter) presenter).setCategroy(categroy);
    }

    @Override
    public void initView() {
        setXRefreshView(mXRefreshView, true, true, false, true, new SmileyHeaderView(frg), new CustomerFooter(frg));
        mRecycleView.setLayoutManager(new LinearLayoutManager(frg));
        mRecycleView.setHasFixedSize(true);
        mOilOrderAdp = new MyOrderAdp(R.layout.adp_my_order, mSaveMyOrders);
        mOilOrderAdp.openLoadAnimation(new CustomAnimation());
        mOilOrderAdp.setNotDoAnimationCount(3);
        mOilOrderAdp.isFirstOnly(false);
        mRecycleView.setAdapter(mOilOrderAdp);
    }

    @Override
    public void initData() {
        loadData(!hasDataInPage(), false);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
        //处理点击事件
        mOilOrderAdp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                MyOrderModel model = (MyOrderModel) adapter.getItem(position);
                //-1全部 1 待支付 4：已支付5已完成 7申请退款
                int orderState = model.getOrderState();
                //产品　类型 3000柴油卡购买  3001 汽油  3003 柴油卡激活 3004柴油批发定金
                String productType = model.getProductType();
                switch (view.getId()) {
                    case R.id.id_oil_recharge_cancel_btn:
                        //油卡充值获得激活,取消订单
                        if (TextUtils.equals("3000", productType) || TextUtils.equals("3003", productType)) {
                            if (orderState == 1) {
                                //取消订单
                                ((CardGroupPresenter) presenter).cancelPayOrder(model.getOrderId());
                            }
                        }
                        break;
                    case R.id.id_oil_recharge_pay_btn:
                        //油卡充值获得激活,去支付,重新下单
                        if (TextUtils.equals("3000", productType) || TextUtils.equals("3003", productType)) {
                            if (orderState == 1) {
                                showActivity(frg, OilSetMealAty.class);
                            }
                        }
                        break;
                    case R.id.id_oil_company_txt:
                        //进入公司详情
                        if (TextUtils.equals("3004", productType)) {
                            Intent intent = new Intent(frg, SearchOilAty.class);
                            intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_OILMENU);
                            intent.putExtra(IntentKey.General.KEY_DES, model.orgNickName);
                            showActivity(frg, intent);
                        }
                        break;
                    case R.id.id_jump_detail_linear:
                        //进入柴油批发详情
                        if (TextUtils.equals("3004", productType)) {
                            Intent details = new Intent(frg, OilDetailAty.class);
                            details.putExtra(IntentKey.General.KEY_ID, model.uid);
                            showActivity(frg, details);
                        }
                        break;
                    case R.id.id_cancel_order_btn:
                        //柴油批发处理按钮
                        if (TextUtils.equals("3004", productType)) {
                            if (orderState == 1) {
                                //取消订单
                                ((CardGroupPresenter) presenter).cancelPayOrder(model.getOrderId());
                            } else if (orderState == 4 || orderState == 7) {
                                //查看订单详情
                                Intent intent = new Intent(frg, OilOrderDetailAty.class);
                                intent.putExtra(IntentKey.General.KEY_ID, model.getOrderId());
                                showActivity(frg, intent);
                            }
                        }
                        break;
                    case R.id.id_ask_back_money_btn:
                        //柴油批发处理按钮
                        if (TextUtils.equals("3004", productType)) {
                            if (orderState == 1) {
                                //联系客服
                                StringUtils.telPhone(frg, "400 863 1886");
                            } else if (orderState == 4) {
                                //申请退款
                                ((CardGroupPresenter) presenter).askBackMoneyOrder(model.getOrderId());
                            } else if (orderState == 7) {
                                //已申请退款
                                SVProgressHUD.showInfoWithStatus(frg, "退款已在申请中");
                            } else if (orderState == 5) {
                                //查看订单详情
                                Intent intent = new Intent(frg, OilOrderDetailAty.class);
                                intent.putExtra(IntentKey.General.KEY_ID, model.getOrderId());
                                showActivity(frg, intent);
                            }
                        }
                        break;
                    case R.id.id_call_kefu_btn:
                        //柴油批发处理按钮
                        if (TextUtils.equals("3004", productType)) {
                            if (orderState == 1) {
                                //联系客服
                                StringUtils.telPhone(frg, "400 863 1886");
                            } else if (orderState == 4 || orderState == 7) {
                                //联系客服
                                StringUtils.telPhone(frg, "400 863 1886");
                            } else if (orderState == 5) {
                                //去评价
                                Intent intent = new Intent(frg, CommentListAty.class);
                                intent.putExtra(IntentKey.General.KEY_ID, model.uid);
                                showActivity(frg, intent);
                            }
                        }
                        break;
                }
            }
        });
    }

    /**
     * 加载数据
     *
     * @param loadMore 是否加载更多
     */
    public void loadData(boolean isShowLoad, boolean loadMore) {
        ((CardGroupPresenter) presenter).getMyOilCards(isShowLoad, loadMore, categroy + "");
        if (!loadMore) {
            stopRefresh(mXRefreshView, 4000);
        } else {
            stopLoadMore(mXRefreshView, 4000);
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.CANCEL_ORDER_SUCCESS)) {
                SVProgressHUDDismiss(frg);
                //取消订单成功
//                mXRefreshView.startRefresh();
                loadData(false, false);
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.MY_OIL_CARDS)) {
                //我的订单
                List<MyOrderModel> mMyOrders = (List<MyOrderModel>) t;
                if (null != mMyOrders) {
                    if (mMyOrders.isEmpty()) {
                        if (loadMore) {
                            ToastUtil.showToast("没有更多数据");
                            mXRefreshView.setLoadComplete(true);
                        } else {
                            //暂无数据
                            mLinearError.setVisibility(View.VISIBLE);
                            mRecycleView.setVisibility(View.GONE);
                        }
                    } else {
                        mLinearError.setVisibility(View.GONE);
                        mRecycleView.setVisibility(View.VISIBLE);
                        if (!loadMore) {
                            mSaveMyOrders.clear();
                        }
                        mSaveMyOrders.addAll(mMyOrders);
                        mOilOrderAdp.notifyDataSetChanged();
                    }
                } else {
                    mLinearError.setVisibility(View.VISIBLE);
                    mRecycleView.setVisibility(View.GONE);
                }
            }
        }
        if (!loadMore) {
            stopRefresh(mXRefreshView, 2000);
        } else {
            stopLoadMore(mXRefreshView, 2000);
        }
    }
}
